package com.order.center.controller;

//import com.order.center.config.OrderConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.order.center.client.IPayClient;
import com.order.center.utils.AESUtil;
import com.pay.common.enums.PayChannelEnum;
import com.pay.common.enums.PayWayEnum;
import com.pay.common.req.NetBankPayReq;
import com.pay.common.resp.NetBankPayRespData;
import com.pay.common.resp.PayNotifyData;
import com.pay.common.resp.Response;
import com.pay.common.utils.PayDigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {

//    @Autowired
//    private OrderConfig orderConfig;


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IPayClient payClient;

    @RequestMapping("/order/create")
    public String createOrder(@RequestParam("orderId") String orderId){
        System.out.println("￥￥￥￥￥￥￥￥接收到用户的订单创建请求￥￥￥￥￥ orderId :" + orderId);


//        //aes 做解密
//        try {
//
//            String o = AESUtil.Decrypt(orderId,"1234567890098769");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//
        String cardNo = "6266090991213214321";
//
//        String encCardNo = null;
//
//        try {
//
//            encCardNo = AESUtil.Encrypt(cardNo,"1234567890098769");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        return cardNo;
    }

    /**
     * 付款
     * @return
     */
    @RequestMapping("/toPay")
    public String tt(){
        System.out.println("-----------tt--------------");

        //创建订单


        NetBankPayReq netBankPayReq = new NetBankPayReq();

        netBankPayReq.setMchId("1000");
        netBankPayReq.setOrderAmt("100");
        netBankPayReq.setOrderDesc("test");
        netBankPayReq.setPayChannel(PayChannelEnum.ALIPAY.getCode());
        netBankPayReq.setPayWay(PayWayEnum.ALIPAY_PAGE_PAY.getCode());
        netBankPayReq.setOrderId(System.currentTimeMillis() + "");


        try {


           JSONObject object = (JSONObject)JSON.toJSON(netBankPayReq);

            String sign = PayDigestUtil.generateSignature(object,"test123","MD5");
            System.out.println("sign = " + sign);

            netBankPayReq.setSign(sign);
            netBankPayReq.setSignType("MD5");
        } catch (Exception e) {
            e.printStackTrace();
        }

        Response<NetBankPayRespData> respDataResponse =  payClient.netBankPay(netBankPayReq);

        System.out.println("网银支付接口返回参数 ： " + respDataResponse);


        if(respDataResponse.getCode() != 0){

            //接口处理失败
        }else {

            //正常逻辑

            NetBankPayRespData respData = respDataResponse.getData();

            //验签

            //跳转支付页面


        }

       //


//        //调支付接口， fegin。
//        String str = restTemplate.postForObject("http://127.0.1.1:9999/pay/create",netBankPayReq,String.class);
//        //{code msg data payUrl:}
//
//        //获取到二维码地址

        //


        return "suc"; //orderConfig.getJdbcUrl();
    }
}
